package servlet;

import entity.Vehiclecomplaints;
import service.impl.VehiclecomplaintsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 吴阳基
 * @datetime 2024/01/04 15:06
 */
@WebServlet(urlPatterns = "/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        Vehiclecomplaints vehiclecomplaints = new Vehiclecomplaints();
        vehiclecomplaints.setBrandId(Integer.parseInt(req.getParameter("brandId")));
        vehiclecomplaints.setProblem(req.getParameter("problem"));
        vehiclecomplaints.setCarType(req.getParameter("carType"));
        vehiclecomplaints.setComplaintsTime(req.getParameter("complaintsTime"));
        vehiclecomplaints.setStatus(Integer.parseInt(req.getParameter("status")));

        VehiclecomplaintsServiceImpl vehiclecomplaintsService = new VehiclecomplaintsServiceImpl();
        int add = vehiclecomplaintsService.add(vehiclecomplaints);

        PrintWriter out = resp.getWriter();
        if (add > 0) {
            out.println("<script>alert('添加成功！');location.href = 'vehiclecomplaintsList'</script>");
        } else {
            out.println("<script>alert('添加失败！');history.back()</script>");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
